package example.com.zyfypt_618zgm.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import example.com.zyfypt_618zgm.R;
import example.com.zyfypt_618zgm.bean.LoginBean;
import example.com.zyfypt_618zgm.model.LoginModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 小茗同学 on 2017/5/8.
 */

public class Denglu  extends AppCompatActivity implements View.OnClickListener{
    private Button button;
    private Button button2;
    private EditText edit;
    private EditText edit1;
    private CheckBox cheak;
    private SharedPreferences sp;
    public static String SessionID;
    public static String username;
    public static String getSessionID() {
        return SessionID;
    }
    public static String getUsername() {
        return username;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiangduibuju);
        edit=(EditText) findViewById(R.id.editText);
        edit1=(EditText) findViewById(R.id.editText2);
        button=(Button)findViewById(R.id.button);
        button2=(Button)findViewById(R.id.button2);
        cheak=(CheckBox) findViewById(R.id.checkBox);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        if(sp.getString("cheak1","false").equals("true")){
            edit.setText(sp.getString("username",""));
            edit1.setText(sp.getString("password",""));
            cheak.setChecked(true);
        }
        else{
            edit.setText("");
            edit1.setText("");
            cheak.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.button:
            call();
            break;
        case R.id.button2:
            Intent zhuce=new Intent();
            zhuce.setClass(Denglu.this,Zhuce.class);
            startActivity(zhuce);
            break;
        default:break;
    }
}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        commit();
    }

    private void commit(){
        SharedPreferences.Editor editor = sp.edit();
        if(cheak.isChecked()) {
            editor.putString("username", edit.getText().toString());
            editor.putString("password", edit1.getText().toString());
            editor.putString("cheak1", "true");
        }else{
            editor.putString("username", "");
            editor.putString("password", "");
            editor.putString("cheak1", "false");
        }
        editor.commit();
    }

    private void call(){
        LoginModel loginModel = new LoginModel();
        Call<LoginBean> call=loginModel.getResult(edit.getText().toString(), edit1.getText().toString());
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    SessionID=response.body().getSessionid();
                    if(SessionID!=null)
                    {
                        commit();
                        username=edit.getText().toString();
                        Intent intent1=new Intent();
                        intent1.setClass(Denglu.this,Main.class);
                        startActivity(intent1);
                    }else {
                        Toast.makeText(Denglu.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(Denglu.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                Toast.makeText(Denglu.this,"登陆失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
